<!--
@license
Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../vz-line-chart/vz-line-chart.html">

<dom-module id="tf-debugger-line-chart">
  <template>
    <vz-line-chart
      x-components-creation-method="[[_lineChartXComponentsCreationMethod]]"
      y-value-accessor="[[_lineChartYValueAccessor]]"
      tooltip-columns="[[_lineChartTooltipColumns]]"
      smoothing-enabled="[[_lineChartSmoothingEnabled]]"
    ></vz-line-chart>
    <style>
      vz-line-chart {
        height: 300px;
        position: relative;
      }
    </style>
  </template>

  <script>
    Polymer({
      is: "tf-debugger-line-chart",
      properties: {
        // x and y data for the line plot, with keys 'x' and 'y'. The values
        // are arrays of numbers.
        data: {
          type: Object,
          value: null,
        },

        _defaultSeriesName: {
          type: String,
          value: '__debugger_data__',
          readonly: true,
        },

        _lineChartXComponentsCreationMethod: {
          type: Object,
          readOnly: true,
          value: () => (() => {
            const scale = new Plottable.Scales.Linear();
            return {
              scale: scale,
              axis: new Plottable.Axes.Numeric(scale, 'bottom'),
              accessor: d => d.step,
            };
          }),
        },
        _lineChartYValueAccessor: {
          type: Object,
          readOnly: true,
          // This function returns a function because polymer calls the outer
          // function to compute the value. We actually want the value of this
          // property to be the inner function.
          value: () => (d => d.scalar),
        },
        _lineChartTooltipColumns: {
          type: Array,
          readOnly: true,
          value: () => {
            return [
              {
                title: 'Name',
                evaluate: (d) => {
                  return 'step=' + d.datum.step + '; scalar= ' + d.datum.scalar;
                },
              },
            ];
          },
        },
        _lineChartSmoothingEnabled: {
          type: Boolean,
          value: false,
          readOnly: true,
        },

      },

      observers: [
        "render(data)",
        // TODO(cais): Add additional data such as yAxis and title.
      ],

      render(data) {
        if (data == null) {
          return;
        }
        this.$$('vz-line-chart').setVisibleSeries([this._defaultSeriesName]);
        let seriesData = [];
        const xData = data['x'];
        const yData = data['y'];
        for (let i = 0; i < xData.length; ++i) {
          seriesData.push({'step': xData[i], 'scalar': yData[i]});
        }
        this.$$('vz-line-chart').setSeriesData(this._defaultSeriesName, seriesData);
      },
    });
  </script>

</dom-module>
